<?php

namespace App\Console\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class ShardingsphereInitEncryptDataCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'shardingsphere:init';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Init ShardingSphere Databases Origin Data to Encrypt';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $this->processShopMemberAddress();
    }
    private function processShopMemberAddress(){
        $table = "member_address";
        $fields=['name','mobile','address','full_address'];
        $total=0;
        $size= DB::connection("shop")->table($table)->count();
        echo <<<EOL
         ==================================
          【{$table}】共计：【{$size}】
         ==================================
        EOL;
        DB::beginTransaction();
        //use 引入外部基础类型变量需要加&引用符号方可改写其数值
        DB::connection("shop")->table($table)->orderBy('id','asc')->chunk(100,function($addrs) use(&$total,$table){
            foreach($addrs as $addr){
                $update=[];
                if($this->avalidStr($addr->name))
                $update['name']=$addr->name;
                if($this->avalidStr($addr->mobile))
                $update['mobile']=$addr->mobile;
                if($this->avalidStr($addr->address))
                $update['address']=$addr->address;
                if($this->avalidStr($addr->full_address))
                $update['full_address']=$addr->full_address;
                if(!empty($update)){
                try{
                    $v=DB::connection("shardingsphere")->table($table)->where('id',$addr->id)->update($update);
                    $total+=$v;
                  }catch(Exception $e){
                    DB::rollBack();
                    echo $e->getMessage().PHP_EOL;
                    return -1;
                  }
                }
            }
        });
        echo <<<EOL
         ==================================
          【{$table}】处理：【{$total}】
         ==================================
        EOL;
        DB::commit();
        return 0;
    }
    private function processShopMember(){
        $table = "member";
        $fields=['username','nickname','realname','mobile'];
        $total=0;
        $size= DB::connection("shop")->table($table)->count();
        echo <<<EOL
         ==================================
          原始数据共计：【{$size}】
         ==================================
        EOL;
        DB::beginTransaction();
        //use 引入外部基础类型变量需要加&引用符号方可改写其数值
        DB::connection("shop")->table($table)->orderBy('member_id','asc')->chunk(100,function($members) use(&$total,$table){
            foreach($members as $member){
                $update=[];
                if($this->avalidStr($member->username))
                    $update['username']=$member->username;
                if($this->avalidStr($member->nickname))
                    $update['nickname']=$member->nickname;
                if($this->avalidStr($member->realname))
                    $update['realname']=$member->realname;
                if($this->avalidStr($member->mobile))
                    $update['mobile']=$member->mobile;
                if(!empty($update)){
                try{
                    $v=DB::connection("shardingsphere")->table($table)->where('member_id',$member->member_id)->update($update);
                    $total+=$v;
                  }catch(Exception $e){
                    DB::rollBack();
                    echo $e->getMessage().PHP_EOL;
                    return -1;
                  }
                }
            }
        });
        echo <<<EOL
           处理数据共计：【{$total}】\r\n
        EOL; 
        DB::commit();
        return 0;
    }
    private function avalidStr($s){
        $s=preg_replace('/\s*/','',$s);
        return !empty($s);
    }
}
